<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>附件列表</title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
</head>
<script type="text/javascript">
Ext.Loader.setConfig({enabled: true});

Ext.Loader.setPath('Ext.ux', '/ext4/ux/');
Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.toolbar.Paging',
    'Ext.ModelManager',
    'Ext.tip.QuickTipManager'
]);


function delAttachFile(file_id) {
    var params = {};
    params._id = _id;
    params.file_id = file_id;
    console.log(params);
    Ext.Ajax.request({
        url: "/article/del/attach",
        params: params,
        method: 'POST',
        success: function(response, opts) {
            alert('上传成功!');
            store.loadPage(1);
        },
        failure: function(response, opts) {
            // alert('业务处理失败');
            Ext.MessageBox.alert('消息提示', Ext.JSON.decode(response.responseText).error)
        }
    });
}
var win, uploadForm,_id,store;
Ext.onReady(function () {
    var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
    _id = _urlparams._id;
    
    if(!_id){
        alert("无法找到文件id");
        return;
    }

    function addAttachFile() {
        uploadFileCb(uploadForm, "","", function(err, file) {
            if (err) {
                alert(err);
            } else {
                var params = {};
                params.file_id = file.file_id;
                params.file_name = file.file_name;
                params.file_size = file.file_size;
                params._id = _id;
                Ext.Ajax.request({
                    url: "/article/add/attach",
                    params: params,
                    method: 'POST',
                    success: function(response, opts) {
                        alert('上传成功!');
                        store.loadPage(1);
                    },
                    failure: function(response, opts) {
                        // alert('业务处理失败');
                        Ext.MessageBox.alert('消息提示', Ext.JSON.decode(response.responseText).error)
                    }
                });
            }
        })
    }



    Ext.define( 'FileList', {
        extend: 'Ext.data.Model',
        fields: [
            '_id','file_name','file_id','file_size','upload_date'
        ],
        idProperty: '_id'
    });

    // create the Data Store
    store = Ext.create('Ext.data.Store', {
        pageSize: 20,
        model: 'FileList',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            url: '/article/detail?_id=' + _id,
            reader: {
                type: 'json',
                root: 'file_list',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: '_id',
                direction: 'DESC'
            }
        ]
    });

    uploadForm = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        region: 'north',
        height : 100,
        defaultType: 'textfield',
        layout: {
            type: 'table',
            columns: 4
        },
        items: [{
            xtype: "filefield",
            name: 'attach_file',
            id : 'attach_file',
            width: 300
        }, {
            xtype: "label",
            width : 50,
            value : "hello "
        },
        {
            xtype : 'button',
            icon: '../img/add.png',
            text: "添  加",
            width : 100,
            handler: addAttachFile
        }]
    });

    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {text: "_id",dataIndex: '_id',hidden:true},
            {text: "文件id",dataIndex: 'file_id'},
            {text: '附件名称',dataIndex: 'file_name',width:200},
            {text: '附件大小',dataIndex: 'file_size',width:200},
            {text: '上传时间',dataIndex: 'upload_date',width:200},
            {
                text: '操作',
                dataIndex: 'file_id',
                width:100,
                renderer:function(val){         
                    val = "'" + val + "'";
                    return '<a onclick="delAttachFile(' + val + ');">删  除</a>';
                }
            }
        ],
        // paging bar on the bottom
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: 'Displaying {0} - {1} of {2}',
            emptyMsg: "没有数据！！！"

        })
    });


    var viewport = new Ext.Viewport({
        layout: 'border',
        renderTo: "topic-grid",
        items: [uploadForm,grid]}
    );

    function doSearch(){
        store.loadPage(1,{params:uploadForm.getValues()});
    }
    // trigger the data store load
    store.on('beforeload',function(store, operation,eOpts){
        operation.params= uploadForm.getValues();
    },this);
    store.loadPage(1);
    window.refresh = function(){
        store.loadPage(1);
    }

    window.closeWin = function(){
        win.hide();
    }
});
</script>
<body>
<div id="topic-grid" style="height: 1024px;width:100%"></div>
</body>
</html>